package com.itcrx.emalladmin.service.impl;

import com.itcrx.emalladmin.constant.MessageConstant;
import com.itcrx.emalladmin.exception.AccountLockedException;
import com.itcrx.emalladmin.exception.AccountNotFoundException;
import com.itcrx.emalladmin.exception.PasswordErrorException;
import com.itcrx.emalladmin.mapper.UsersMapper;
import com.itcrx.emalladmin.model.dto.UsersLoginDTO;
import com.itcrx.emalladmin.model.entity.Users;
import com.itcrx.emalladmin.service.IUsersService;
import org.apache.logging.log4j.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author NoobMaster69  * @date 2024/6/5 0005 星期三 9:42
 */
@Service
public class UsersServiceImpl implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 用户登录
     * @param usersLoginDTO
     * @return
     */
    @Override
    public Users login(UsersLoginDTO usersLoginDTO){
        String userLogin = usersLoginDTO.getUserLogin();
        String userPass = usersLoginDTO.getUserPass();
        //1、根据用户名查询数据库中的数据
        Users users = usersMapper.getByUserLogin(userLogin);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if(users == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        //对前端传过来的密码进行md5处理
        userPass = DigestUtils.md5DigestAsHex(userPass.getBytes());
        if (!userPass.equals(users.getUserPass())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return users;
    }


}
